package com.shen.core.web;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @Author: shenyang
 * @CreateTime: 2022-09-30  13:26
 * @Description: TODO
 * @Version: 1.0
 */
@ControllerAdvice
public class ControllerExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(ControllerExceptionHandler.class);

    @ExceptionHandler(Throwable.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResultData<String> exceptionHandler(Throwable e) throws Throwable { // NOSONAR
        // 将AccessDeniedException抛出，交给Spring Security的ExceptionTranslationFilter处理
        logger.error("异常返回", e);

        return ResultFactory.build(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
    }

    /**
     * Assert异常
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public ResultData<String> exception(IllegalArgumentException e) {
        return ResultFactory.build(HttpStatus.BAD_REQUEST.value(), e.getMessage());
    }
    

}
